<template>
  <a-modal
    :visible="show"
    width="100%"
    title="获取物资"
    @ok="handleConfirm"
    @cancel="handleClose"
    :maskClosable="false"
    :key="show"
    wrapClassName="full-modal"
  >
    <a-form-model :model="searchForm">
      <div class="filter-container" style="display: flex; justify-content: space-between;">
        <a-row  style="display: flex;flex:1;margin-right:15px;">          
          <a-form-model-item
            class="searchForm-item"
            label="预留单号:"
            v-if="consum.materialsType != '2' && consum.materialsType != '3'"
          >
            <a-auto-complete
              v-model="searchForm.orderNo"
              :data-source="orderNoSuggetions"
              class="inline-input"
              @select="onSelect"
              @search="onSearch"
              @change="onChange"
            />
          </a-form-model-item>
          <a-form-model-item label="物料编码:" class="searchForm-item">
            <a-input v-model="searchForm.materialsNo" />
          </a-form-model-item>
          <a-form-model-item label="物料名称:" class="searchForm-item" v-if="consum.materialsType != '2' && consum.materialsType != '3'">
            <a-input v-model="searchForm.materialsRemark" />
          </a-form-model-item>
          <a-form-model-item
            label="工厂:"
            class="searchForm-item"
            v-if="consum.materialsType != '1' && consum.materialsType != '2' && consum.materialsType != '3'"
          >
            <a-input v-model="searchForm.factory" />
          </a-form-model-item>
          <a-form-model-item label="库存地点:" class="searchForm-item">
            <a-input v-model="searchForm.materialsAddr" />
          </a-form-model-item>
        </a-row>        

        <div>
          <a-button type="primary" @click="handleSearch" style="height: 38px; margin-right: 5px" :loading="loading"
            >获取物资库存数据</a-button
          >
          <!-- <a-button @click="handleDownloadExcel" style="height: 38px">导出</a-button> -->
        </div>
      </div>
    </a-form-model>
    <div style="margin:20px 0">
      <a-table
        class="hte-table-content"
        :columns="columns[consum.materialsType]"
        :data-source="records"
        :pagination="false"
        :rowKey="(record) => record.randomStr"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :customRow="customRow"
        bordered
        size="small"
        :scroll="{ x: 2620, y: '45vh'}"
      >
        <template slot="bdmngRender" slot-scope="text, record, index">
          <a-input v-model="record.bdmng" @blur="changeBdmng(index,record)" />
        </template>
        <template slot="sobkzRender" slot-scope="text, record">
          <div v-if="record.sobkz == '10'">Y</div>
        </template>
        <template slot="kostlRender" slot-scope="text, record">
          <span v-if="record.kostl != '0'" class="font-active" @click="toDetail('costCenter', record.kostl)">
            {{ record.kostl }}</span
          >
        </template>
        <template slot="matnrRender" slot-scope="text, record">
          <span class="font-active" @click="toDetail('materialsNo', record.matnr)"> {{ record.matnr }}</span>
        </template>
        <template slot="posidRender" slot-scope="text, record">
          <span class="font-active" @click="toDetail('wbsNo', record.posid)"> {{ record.posid }}</span>
        </template>
        <template slot="rsDatRender" slot-scope="text, record">
        {{record.bdter}}
        </template>
      </a-table>
    </div>
    <a-pagination
      show-size-changer
      :default-current="1"
      :total="pagination.total"
      @change="pageChange"
      @showSizeChange="onShowSizeChange"
      :show-total="(total) => `共 ${total} 条记录`"
    />
  </a-modal>
</template>

<script>
import { getAction, postAction } from '@/api/manage'
import { formatDate,fermitTime } from '@/utils/util'
import { ColumnWidthMixin } from '@/views/materials/ColumnWidthMixin'
export default {
  mixins: [ColumnWidthMixin],
  name: 'MaterialsSap',
  props: {
  },
  components: {},
  data() {
    return {
      userinfo: {
        info: this.$store.getters.userInfo,
        module: 'ContractTaskList',
      },
      dictMap: [],
      records: [],
      searchForm: {},
      powers: {},
      orderNoSuggetions: [],
      loading: false,
      maktxWidth: '120px',
      materialsTypeOptions: [
        { code: '1', text: '预留' },
        { code: '2', text: 'WBS' },
        { code: '3', text: '公有' },
      ],
      urls: {
        getSap: '/materials/sap',
        getSapWBS: '/materials/sapwbs',
        getSapPub: '/materials/sappub',
        psCodes: '/hte/contract/codes',
      },
      columns: {
        1: [
          {
          title: '序号',
          dataIndex: 'sortIndex',
          width: '60px',
          align: 'center',
          customRender: (text, record, index) => {
            return index + 1
          },
        },
          {
            title: '物资类型',
            dataIndex: 'materialsType',
            width: '100px',
            align: 'center',
          },
          {
            title: '寄售标志',
            dataIndex: 'sobkz',
            width: '100px',
            align: 'center',
            scopedSlots: { customRender: 'sobkzRender' },
          },
          {
            title: '物料编码',
            dataIndex: 'matnr',
            width: '100px',
            align: 'center',
            scopedSlots: { customRender: 'matnrRender' },
          },
          {
            title: '物料名称',
            dataIndex: 'maktx',
            width: '120px',
            align: 'left',
            ellipsis: true,
            // customCell: () => {
            //   return {
            //     style: {
            //       width: this.maktxWidth,
            //       overflow: 'hidden',
            //       whiteSpace: 'nowrap',
            //       textOverflow: 'ellipsis',
            //       cursor: 'pointer',
            //     },
            //   }
            // },
            // customRender: (text, record) => (
            //   <a-tooltip placement="topLeft" title={record.maktx}>
            //     {record.maktx}
            //   </a-tooltip>
            // ),
          },
          {
            title: '单位',
            dataIndex: 'meins',
            width: '60px',
            align: 'center',
          },
          {
            title: '未提预留数量',
            dataIndex: 'noReserveNum',
            width: '120px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '领用数量',
            dataIndex: 'bdmng',
            width: '100px',
            align: 'center',
            scopedSlots: { customRender: 'bdmngRender' },
          },
          {
            title: '领用金额',
            dataIndex: 'totalPrice',
            width: '100px',
            align: 'center',
          },
          // {
          //   title: '可用库存',
          //   dataIndex: 'lessNum',
          //   width: '140px',
          //   align: 'center',
          // },
          {
            title: '提示',
            dataIndex: 'tips',
            width: '80px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '已提货数量',
            dataIndex: 'ENMNG',
            width: '110px',
            align: 'center',
          },
          {
            title: '_可用库存',
            dataIndex: '_lessNum',
            width: '110px',
            align: 'center',
          },
          {
            title: '寄售库存',
            dataIndex: 'KLABS',
            width: '100px',
            align: 'center',
          },
          {
            title: '海天非限制库存',
            dataIndex: 'JPQTY',
            width: '120px',
            align: 'center',
          },
          //  {
          //   title: 'BDMNG',
          //   dataIndex: 'BDMNG',
          //   width: '160px',
          //   align: 'center',
          // },
          // {
          //   title: 'ENMNG',
          //   dataIndex: 'ENMNG',
          //   width: '160px',
          //   align: 'center',
          // },
          {
            title: '需优先预留数量',
            dataIndex: 'reserveNum',
            width: '120px',
            align: 'center',
            ellipsis: true,
          },
          // {
          //   title: '移动单价',
          //   dataIndex: 'price',
          //   width: '120px',
          //   align: 'center',
          // },
          {
            title: '收货方',
            dataIndex: 'wempf',
            width: '100px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '需求部门',
            dataIndex: 'KTEXT',
            width: '100px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '成本中心',
            dataIndex: 'kostl',
            width: '120px',
            align: 'center',
            scopedSlots: { customRender: 'kostlRender' },
          },
          {
            title: 'WBS号',
            dataIndex: 'posid',
            width: '120px',
            align: 'center',
            scopedSlots: { customRender: 'posidRender' },
          },
          {
            title: '项目名称',
            dataIndex: 'projectName',
            width: '120px',
            align: 'center',
            scopedSlots: { customRender: 'posidRender' },
            ellipsis: true,
            // customCell: () => {
            //   return {
            //     style: {
            //       width: '120px',
            //       overflow: 'hidden',
            //       whiteSpace: 'nowrap',
            //       textOverflow: 'ellipsis',
            //       cursor: 'pointer',
            //     },
            //   }
            // },
            // customRender: (text, record) => (
            //   <a-tooltip placement="topLeft" title={record.projectName}>
            //     {record.projectName}
            //   </a-tooltip>
            // ),
          },
          {
            title: '库存地点',
            dataIndex: 'lgort',
            width: '100px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '预留单号',
            dataIndex: 'rsNum',
            width: '100px',
            align: 'center',
          },
          {
            title: '预留日期',
            dataIndex: 'RSDAT',
            width: '100px',
            align: 'center',
            scopedSlots: { customRender: 'rsDatRender' },
          },
          {
            title: '工厂',
            dataIndex: 'werks',
            width: '80px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '地点描述',
            dataIndex: 'lgobe',
            width: '180px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '备注',
            dataIndex: 'remark',
            width: '100px',
            align: 'center',
            ellipsis: true,
          },
        ],
        2: [
          {
          title: '序号',
          dataIndex: 'sortIndex',
          width: '60px',
          align: 'center',
          customRender: (text, record, index) => {
            return index + 1
          },
        },
          {
            title: '物资类型',
            dataIndex: 'materialsType',
            width: '80px',
            align: 'center',
          },
          {
            title: '物料编码',
            dataIndex: 'matnr',
            width: '100px',
            align: 'center',
            scopedSlots: { customRender: 'matnrRender' },
          },
          {
            title: '物料名称',
            dataIndex: 'maktx',
            width: '120px',
            align: 'left',
            ellipsis: true,
          },
          {
            title: '单位',
            dataIndex: 'meins',
            width: '60px',
            align: 'center',
          },
          {
            title: '领用数量',
            dataIndex: 'bdmng',
            width: '80px',
            align: 'center',
            scopedSlots: { customRender: 'bdmngRender' },
          },
          {
            title: '领用金额',
            dataIndex: 'totalPrice',
            width: '80px',
            align: 'center',
          },
          {
            title: '可用库存',
            dataIndex: 'lessNum',
            width: '80px',
            align: 'center',
          },
          {
            title: '项目库存数量',
            dataIndex: 'PRLAB',
            width: '120px',
            align: 'center',
          },
          // {
          //   title: '移动单价',
          //   dataIndex: 'price',
          //   width: '80px',
          //   align: 'center',
          // },
          {
            title: 'WBS号',
            dataIndex: 'posid',
            width: '120px',
            align: 'center',
            scopedSlots: { customRender: 'posidRender' },
          },
          {
            title: '项目名称',
            dataIndex: 'POST1',
            width: '80px',
            align: 'center',
            ellipsis: true,
          },
          // {
          //   title: '项目负责人',
          //   dataIndex: 'projectManager',
          //   width: '120px',
          // },
          {
            title: '库存地点',
            dataIndex: 'lgort',
            width: '80px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '提示',
            dataIndex: 'tips',
            width: '80px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '工厂',
            dataIndex: 'werks',
            width: '60px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '地点描述',
            dataIndex: 'LGOBE',
            width: '120px',
            align: 'center',
            ellipsis: true,
          },
        ],
        3: [
          {
          title: '序号',
          dataIndex: 'sortIndex',
          width: '60px',
          align: 'center',
          customRender: (text, record, index) => {
            return index + 1
          },
        },
          {
            title: '物资类型',
            dataIndex: 'materialsType',
            width: '100px',
            align: 'center',
          },
          {
            title: '物料编码',
            dataIndex: 'matnr',
            width: '100px',
            align: 'center',
            scopedSlots: { customRender: 'matnrRender' },
          },
          {
            title: '物料名称',
            dataIndex: 'maktx',
            width: '120px',
            align: 'left',
            ellipsis: true,
          },
          {
            title: '单位',
            dataIndex: 'meins',
            width: '80px',
            align: 'center',
          },
          {
            title: '领用数量',
            dataIndex: 'bdmng',
            width: '100px',
            align: 'center',
            scopedSlots: { customRender: 'bdmngRender' },
          },
          {
            title: '领用金额',
            dataIndex: 'totalPrice',
            width: '100px',
            align: 'center',
          },
          {
            title: '可用库存',
            dataIndex: 'lessNum',
            width: '100px',
            align: 'center',
          },
          {
            title: '非限制库存数量',
            dataIndex: 'TOTALSTOCK',
            width: '120px',
            align: 'center',
          },
          // {
          //   title: '移动单价',
          //   dataIndex: 'price',
          //   width: '120px',
          //   align: 'center',
          // },
          {
            title: '成本中心',
            dataIndex: 'kostl',
            width: '100px',
            align: 'center',
            scopedSlots: { customRender: 'kostlRender' },
          },
          {
            title: '收货方',
            dataIndex: 'wempf',
            width: '100px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '需求部门',
            dataIndex: 'reciverDept',
            width: '100px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '库存地点',
            dataIndex: 'lgort',
            width: '100px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '提示',
            dataIndex: 'tips',
            width: '80px',
            align: 'center',
            ellipsis: true,
          },
          {
            title: '工厂',
            dataIndex: 'werks',
            width: '80px',
            align: 'center',
            ellipsis: true,
          },
          
        ],
      },
      selectedRowKeys: [],
      selectedRow: [],
      dataSource: [],
      pagination: {
        offset: 0,
        total: 0,
        current: 1,
        pageSize: 20,
        pageSizeOptions: ['10', '20', '30', '40', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
      },
      show:false,
      consum:{}
    }
  },
  created() {
    // this.fetchCode()
  },
  methods: {
    formatDate,
    fermitTime,
    openInit(consum) {
      this.show = true
      this.searchForm = {}
      this.records = []
      this.selectedRow = []
      this.consum = consum
      if(consum.materialsType!='3'){
        this.handleSearch()
      }
    },
    
    fetchCode() {
      postAction(this.urls.psCodes, { codes: ['materials_type'] }).then((res) => {
        if (res.success) {
          this.dictMap = res.result
        }
      })
    },
    fetchSap() {
      let param = {}
      if (this.consum.consumPlanId) {
        param.planId = this.consum.consumPlanId
      } else {
        if (this.consum.costType == '1') {
          param.costCenter = this.consum.costNum
        } else {
          param.wbsNo = this.consum.costNum
        }
      }
      this.loading = true
      postAction(this.urls.getSap, { ...this.searchForm, ...param }).then((res) => {
        this.loading = false
        if (res.code == 200) {
          let list = res.result.records
          this.$set(this.pagination, 'total', res.result.total)
           list.map((element) => {
            element.randomStr = this.getRandomStr(10)
            element.materialsType = '预留物资'
            element.sobkz = element.SOBSL
            element.matnr = element.MATNR
            element.maktx = element.MAKTX
            element.meins = element.MEINS
            element.enmng = element.ENMNG
            element.werks = element.WERKS
            element.lgobe = element.LGOBE
            element.lgort = element.LGORT
            element.posid = element.POSID
            element.pspnr = element.PSPNR
            element.kostl = element.KOSTL
            element.wempf = element.WEMPF
            element.rspos = element.RSPOS
            element.bdter = this.fermitTime(new Date(element.RSDAT))
            element.rsnum = element.RSNUM
            element._lessNum = parseFloat(element.LABST) + parseFloat(element.KLABS)
            element.reserveNum = parseFloat(element.BDMNG) - parseFloat(element.ENMNG) //需优先预留数量
            element.lessNum = element._lessNum;//parseFloat(element.LABST) - element.reserveNum
            element.bdmng = 0
            element.price = element.VERPR
            element.totalPrice = 0
            element.stockNumTotal = parseFloat(element.JPQTY)+parseFloat(element.KLABS)
            element.noReserveNum = element.BDMNG - element.ENMNG //未提预留
            element.tips = ''
            element.remark = ''
            element.rsNum = element.RSNUM
            if (!element.lessNum || element.lessNum < 0) {
              element.lessNum = 0
            }
            if (element.sobkz == '10') {
              element.lgort = '214A'
              element.lgobe = '寄售仓'
            }
          })
          this.showSearchForm = false
          this.records = list
          if (this.records.length <= 0) {
            this.$message.warning('SAP里不存在相关记录! ')
          }
          this.columns[1] = this.calcMaktxWidth(this.records, this.columns[1], ['maktx', 'wempf'])
        } else {
          this.$message.error('获取sap数据失败! ' + res.msg)
        }
      })
    },
    fetchWbs() {
      let param = {}
      if (this.consum.costType == '2') {
        param.wbsNo = this.consum.costNum
      }
      this.loading = true
      postAction(this.urls.getSapWBS, { ...this.searchForm, ...param }).then((res) => {
        this.loading = false
        if (res.code == 200) {
          let list = res.result.records
          this.$set(this.pagination, 'total', res.result.total)
          list.map((element) => {
            element.randomStr = this.getRandomStr(10)
            element.materialsType = '项目物资'
            element.matnr = element.MATNR
            element.maktx = element.MAKTX
            element.meins = element.MSEH3
            element.bdmng = 0
            element.lessNum = element.LABST
            element.price = element.MAPRICE
            element.totalPrice = 0
            element.werks = element.WERKS
            element.posid = element.POSID
            element.lgort = element.LGORT
            element.pspnr = element.PSPNR
            element.stockNumTotal = parseFloat(element.JPQTY)+parseFloat(element.KLABS)
            if (!element.lessNum || element.lessNum < 0) {
              element.lessNum = 0
            }
            if (element.sobkz == '10') {
              element.lgort = '214A'
              element.lgobe = '寄售仓'
            }
          })
          this.showSearchForm = false
          this.records = list
          if (this.records.length <= 0) {
            this.$message.warning('SAP里不存在相关记录! ')
          }
          this.columns[2](this.records, this.columns[2], ['maktx'])
        } else {
          this.$message.error('获取sap数据失败! ' + res.msg)
        }
      })
    },
    fetchPub() {
      let param = {}
      if (this.consum.costType != '1') {
        param.costCenter = this.consum.costNum
      }
      this.loading = true
      postAction(this.urls.getSapPub, { ...this.searchForm, ...param }).then((res) => {
        this.loading = false
        if (res.code == 200) {
          let list = res.result.records
          this.$set(this.pagination, 'total', res.result.total)
          list.map((element) => {
            element.randomStr = this.getRandomStr(10)
            element.materialsType = '公有物资'
            element.matnr = element.MATNR
            element.maktx = element.MAKTX
            element.meins = element.MSEH3
            element.bdmng = 0
            element.lessNum = element.LABST
            element.kostl = element.KOSTL | this.consum.costNum
            element.lgort = element.LGORT
            element.price = element.MAPRICE
            element.totalPrice = 0
            element.tips = ''
            element.werks = element.WERKS
            element.wempf = this.userinfo.info.realname
            if (!element.lessNum || element.lessNum < 0) {
              element.lessNum = 0
            }
            if (element.sobkz == '10') {
              element.lgort = '214A'
              element.lgobe = '寄售仓'
            }
          })
          this.showSearchForm = false
          this.records = list
          if (this.records.length <= 0) {
            this.$message.warning('SAP里不存在相关记录! ')
          }
          this.columns[3] = this.calcMaktxWidth(this.records, this.columns[3], ['maktx', 'wempf'])
        } else {
          this.$message.error('获取sap数据失败! ' + res.msg)
        }
      })
    },
    handleSearch() {
      if (this.loading) {
        return
      }
      if (
        this.consum.materialsType == '1' &&
        !this.consum.costNum &&
        !this.consum.applyPlanId &&
        !this.searchForm.orderNo &&
        !this.searchForm.materialsNo &&
        !this.searchForm.materialsRemark &&
        !this.searchForm.materialsAddr
      ) {
        return this.$message.warning('请至少输入一个条件进行查询')
      }
      if (
        this.consum.materialsType == '2' &&
        !this.consum.costNum &&
        !this.consum.consumPlanId &&
        !this.searchForm.materialsNo &&
        !this.searchForm.materialsRemark &&
        !this.searchForm.materialsAddr
      ) {
        return this.$message.warning('请至少输入一个条件进行查询')
      }
      if (
        this.consum.materialsType == '3' &&
        !this.searchForm.materialsNo &&
        !this.searchForm.materialsRemark &&
        !this.searchForm.materialsAddr &&
        !this.searchForm.factory
      ) {
        return this.$message.warning('请至少输入一个条件进行查询')
      }
      if (this.searchForm.materialsAddr) {
        let list = JSON.parse(window.localStorage.getItem('materialsAddr'))
        if (!list) {
          list = []
        }
        if (list.indexOf(this.searchForm.materialsAddr) > -1) {
          list.splice(list.indexOf(this.searchForm.materialsAddr), 1)
        }
        list.splice(0, 0, this.searchForm.orderNo)
        if (list.length > 10) {
          this.orderNoSuggetions = list.slice(0, 10)
        } else {
          this.orderNoSuggetions = list
        }
        window.localStorage.setItem('materialsAddr', JSON.stringify(this.orderNoSuggetions))
      }

      this.records = []
      this.loading = true
      if (!this.pagination.current || this.pagination.current < 1) {
        this.$set(this.pagination, 'current', 1)
      }
      let offset = (this.pagination.current - 1) * this.pagination.pageSize
      this.$set(this.searchForm, 'planId', this.consum.applyPlanId)
      this.$set(this.searchForm, 'offset', offset)
      this.$set(this.searchForm, 'rows', this.pagination.pageSize)
      if (this.consum.materialsType == '1') {
        this.fetchSap()
      }
      if (this.consum.materialsType == '2') {
        this.fetchWbs()
      }
      if (this.consum.materialsType == '3') {
        this.fetchPub()
      }
    },
    closeCostPowerSelector(data) {
      this.showCostPowerSelector = false
      if (data) {
        this.searchForm.costCenter = data.cKostl
        this.handleSearch()
      }
    },
    handleClose() {
      this.show = false
      this.$emit('close')
    },
    handleDownloadExcel() {},
    headerSortClass({ row, column, rowIndex, columnIndex }) {
      // 判断找到当前列 添加样式
      if (this.sort_column[column.id]) {
        return this.sort_column[column.id].order
      }
    },
    headerClick(column, event) {
      let sortParam = {}
      if (this.sort_column[column.id]) {
        sortParam = this.sort_column[column.id]
      }
      sortParam.prop = column.property
      sortParam.order = column.order
      sortParam.id = column.id

      this.sort_column[sortParam.id] = sortParam
      this.handleSort()
    },
    handleSort() {
      let sort_column_list = []
      for (let key in this.sort_column) {
        if (this.sort_column[key].order != null && this.sort_column[key] != '') {
          sort_column_list.push(this.sort_column[key])
        }
      }
      sort_column_list.sort((a, b) => {
        return a['id'] > b['id']
      })
      let sort_prop = []
      let sort_order = []
      for (let sortParam of sort_column_list) {
        if (sortParam.order != null && sortParam.order != '') {
          sort_prop.push(sortParam.prop)
          sort_order.push(sortParam.order)
        }
      }
      this.records = this.orderBy(this.records, sort_prop, sort_order) //
    },
    orderBy(arr, props, orders) {
      arr.sort((a, b) =>
        props.reduce((acc, prop, i) => {
          if (acc === 0) {
            const [p1, p2] = orders && orders[i] === 'descending' ? [b[prop], a[prop]] : [a[prop], b[prop]]
            acc = p1 > p2 ? 1 : p1 < p2 ? -1 : 0
          }
          return acc
        }, 0)
      )
      return arr
    },
    handleSelectionChange(val) {
      this.selectedRow = val
    },
    handleConfirm() {
      this.show = false
      let check = true
      if(this.selectedRow){
        for(let row of this.selectedRow){
          if(row.bdmng<=0){
            check = false
            return this.$message.warning('领用数量<=0,不符合规范，请确认。')
          }
        }
      }
      if(check){
        this.$emit('close', this.selectedRow)
      }
      
    },
    changeMaterialsType() {
      if (this.searchForm.materials_type == '2') {
        this.$set(this.searchForm, 'planId', null)
        this.$set(this.searchForm, 'planNum', null)
        this.$set(this.searchForm, 'costCenter', null)
        this.$set(this.searchForm, 'orderNo', null)
        this.$set(this.searchForm, 'factory', null)
      }
      if (this.searchForm.materials_type == '3') {
        this.$set(this.searchForm, 'planId', null)
        this.$set(this.searchForm, 'planNum', null)
        this.$set(this.searchForm, 'wbsNo', null)
        this.$set(this.searchForm, 'orderNo', null)
        this.$set(this.searchForm, 'materialsRemark', null)
      }
    },
    changeBdmng(index,record) {
      let bdmng = this.records[index].bdmng
      if (bdmng) {
        bdmng = bdmng.trim()
      } else {
        bdmng = 0
      }
      while (bdmng.length > 1 && bdmng.startsWith('0')) {
        bdmng = bdmng.substring(1, bdmng.length)
      }
      this.$set(this.records[index], 'bdmng', parseFloat(bdmng))
      if (isNaN(this.records[index].bdmng)) {
        this.$set(this.records[index], 'bdmng', 0)
      }
      if(this.records[index].sobkz == '10'){
        if (parseFloat(this.records[index].bdmng) > parseFloat(this.records[index].noReserveNum)) {
          this.$set(this.records[index], 'bdmng', this.records[index].noReserveNum)
        }
      }else{
         if (parseFloat(this.records[index].bdmng) > parseFloat(this.records[index].lessNum)) {
          this.$set(this.records[index], 'bdmng', this.records[index].lessNum)
        }
      }
      if (parseFloat(this.records[index].bdmng) < 0) {
        this.$set(this.records[index], 'bdmng', 0)
      }
      if(bdmng>0){
        let newRecord = true
        this.selectedRowKeys.forEach(k=>{
          if(k.randomStr == record.randomStr){
            newRecord = false
          }
        })
        if(newRecord){
          this.selectedRowKeys.push(record.randomStr)
          this.selectedRow.push(record);
        }
      }else{
        let newRecord = true
        this.selectedRowKeys.forEach(k=>{
          if(k == record.randomStr){
            newRecord = false
          }
        })
        if(!newRecord){
          let selectedRowKeysTemp = []
          let selectedRowTemp = []
          this.selectedRowKeys.forEach(k=>{
            if(k != record.randomStr){
              selectedRowKeysTemp.push(k)
            }
          })
          this.selectedRow.forEach(r=>{
            if(r.randomStr != record.randomStr){
              selectedRowTemp.push(r)
            }
          })
          this.selectedRowKeys = selectedRowKeysTemp
          this.selectedRow = selectedRowTemp
        }
      }
      this.$set(this.records[index], 'totalPrice', (this.records[index].price * this.records[index].bdmng).toFixed(3))
    },
    querySearchWbs(queryString, cb) {
      var wbs = this.powers.wbs
      var results = []
      wbs.map((element) => {
        if (element.toLowerCase().indexOf(queryString.toLowerCase()) > -1) {
          let item = {}
          item.value = element
          results.push(item)
        }
      })
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    querySearchCostCenter(queryString, cb) {
      var kostl = this.powers.kostl
      var results = []
      kostl.map((element) => {
        if (element.toLowerCase().indexOf(queryString.toLowerCase()) > -1) {
          let item = {}
          item.value = element
          results.push(item)
        }
      })
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    querySearchOrderNo(queryString, cb) {
      var orderNos = this.orderNoSuggetions
      var results = []
      orderNos.map((element) => {
        if (element.toLowerCase().indexOf(queryString.toLowerCase()) > -1) {
          let item = {}
          item.value = element
          results.push(item)
        }
      })
      cb(results)
    },
    handleOk() {},
    onSearch(searchText) {
      this.dataSource = !searchText ? [] : [searchText, searchText.repeat(2), searchText.repeat(3)]
    },
    onSelect(value) {
      console.log('onSelect', value)
    },
    onChange(value) {
      console.log('onChange', value)
    },
    getRandomStr(len) {
      len = len || 32
      var $chars =
        'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
      var maxPos = $chars.length
      var pwd = ''
      for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
      }
      return pwd
    },
    onSelectChange(selectedRowKeys, selectedRow) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRow = selectedRow
    },
    customRow(record) {
      return {
        on: {
          dblclick: (event) => {
            let newKeyRows = []
            let exist = false
            this.selectedRowKeys.forEach((k) => {
              if (k != record.randomStr) {
                newKeyRows.push(k)
              } else {
                exist = true
              }
            })
            if (!exist) {
              newKeyRows.push(record.randomStr)
            }
            this.selectedRowKeys = newKeyRows

            let newRows = []
            this.selectedRow.forEach((k) => {
              if (k != record.randomStr) {
                newRows.push(k)
              }
            })
            if (!exist) {
              newRows.push(record)
            }
            this.selectedRow = newRows
          },
        },
      }
    },
    toDetail(type, no) {
      let urlJump = this.$router.resolve({ path:'/materials/detail/' + type + '/' + no })
      window.open(urlJump.href, '_blank')
    },
    pageChange(current, pageSize) {
      this.$set(this.pagination, 'pageSize', pageSize)
      this.$set(this.pagination, 'current', current)
      this.handleSearch()
    },
    onShowSizeChange(current, pageSize) {
      this.$set(this.pagination, 'pageSize', pageSize)
      this.$set(this.pagination, 'current', 1)
      this.handleSearch()
    },
  },
}
</script>

<style lang="less" scoped>
@import '~@assets/less/hte-main.less';
.demo-table-expand .a-form-model-item {
  margin-right: 0;
  margin-bottom: 0px;
}
.demo-table-expand span {
  display: inline-block;
  white-space: nowrap;
  width: 95%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.a-form-model-item /deep/ .a-form-model-item--mini .a-form-model-item__label {
  color: #606266 !important;
  font-size: 12px !important;
  font-weight: normal;
}
.a-table /deep/ .a-table__expanded-cell[class*='cell'] {
  padding: 0px 0px 10px 0px;
}

.expand-title {
  height: 30px;
  line-height: 30px;
  background-color: #f1f1f1;
  padding-left: 50px;
  margin-bottom: 5px;
  font-size: 12px;
}
.a-form-model-item /deep/ .a-form-model-item__content {
  height: 36px;
}
.ellipse-tooltips {
  display: inline-block;
  white-space: nowrap;
  width: 95%;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ellipse-tooltips span {
  width: unset !important;
}
.a-range /deep/ .a-range-separator {
  width: 20px;
}
.searchForm {
  display: flex;
  flex-direction: row;
}
.searchForm-left {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: flex-start;
  width: 85vw;
}
.searchForm-right {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  min-width: 280px;
  border-left: #cccccc 2px solid;
}
.searchForm-item{margin-right: 5px}
</style>
<style lang="less">
.full-modal {
  .ant-modal {
    max-width: 100%;
    top: 0;
    padding-bottom: 0;
    margin: 0;
  }
  .ant-modal-content {
    display: flex;
    flex-direction: column;
    height: calc(100vh);
  }
  .ant-modal-body {
    flex: 1;
  }
}
</style>

